package com.dengqi.mymodule.activity.fragment.tabpager;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.dengqi.mymodule.R;
import com.google.android.material.tabs.TabLayout;

import java.util.List;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

/**
 * @Author: dengqi
 * @CreateDate: 2020/11/18 9:17
 * @Description: viewpager+tablayout模式
 */
public class QTabPager extends LinearLayout {

    private QViewPager mViewPager;
    private TabLayout mTabLayout;
    private QViewPagerAdapter mAdapter;

    public QTabPager(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setOrientation(LinearLayout.VERTICAL);
        View view = LayoutInflater.from(context).inflate(R.layout.view_q_tabpager, this, true);
        mViewPager = view.findViewById(R.id.viewPager);
        mTabLayout = findViewById(R.id.tabLayout);
    }

    public void setScrollEnable(boolean isScroll) {
        mViewPager.setScroll(isScroll);
    }

    public void bindData(FragmentManager fragmentManager, List<TabBean> tabList) {
        mAdapter = new QViewPagerAdapter(fragmentManager);
        mViewPager.setAdapter(mAdapter);
        mAdapter.setTabList(tabList);
        mAdapter.notifyDataSetChanged();
        mTabLayout.setupWithViewPager(mViewPager);

//        for (int i = 0; i < mTabLayout.getTabCount(); i++) {
//            TabLayout.Tab tab = mTabLayout.getTabAt(i);
//            tab.setCustomView(createTabView(i));
//        }
    }



//    public View createTabView(int position) {
//        View v = LayoutInflater.from(getContext()).inflate(R.layout.item_bottom, null);
//        TextView textView = v.findViewById(R.id.tv_title);
//        ImageView imageView = v.findViewById(R.id.iv_icon);
//        textView.setText(datas.get(position));
//        imageView.setImageResource(R.mipmap.ic_launcher);
//        //添加一行，设置颜色
//        textView.setTextColor(tabLayout.getTabTextColors());
//        return v;
//    }

    public static class Tab {
        private String name;
    }

    public static class TabBean {
        private String title;
        private int imgRes;
        private Fragment fragment;

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public int getImgRes() {
            return imgRes;
        }

        public void setImgRes(int imgRes) {
            this.imgRes = imgRes;
        }

        public Fragment getFragment() {
            return fragment;
        }

        public void setFragment(Fragment fragment) {
            this.fragment = fragment;
        }
    }
}
